const { controller } = require("../controllers/Controller")

// pages/myCenter/shopGoodsDetail.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
      couponBagId:'',
      goodsId:'',
      shopId:'',
      couponBagGoodsId:'',
      shopDetail:'',
      show:false,
      show2:false
  },

  //获取弹窗消息
  getMsg(){
    let that=this
    controller.getNotice().then(res=>{
      this.setData({
        msg:res.data,
        show2:true
      })

      setTimeout(function(){
        that.setData({
          show2:false
        })
      },6500)
    })
  },

  getGoodsDetail(){
    let value=new Object()
    value.goodsId=this.data.goodsId
    value.shopId=this.data.shopId
    controller.goodsDetails(value).then(res=>{
      this.setData({
        detail:res.data
      })
    })
  },
  //创建核销码
  createCode(){
    let value={}
    let that=this
    value.couponBagGoodsId=this.data.couponBagGoodsId
    controller.createWriteOffGoodsQrCode(value).then(res=>{
      this.setData({
        code: res.data.code,
        codeUrl:res.data.qrCode,
        show:true
      })

     if(!this.data.timer){
      this.setData({
        timer:setInterval(function(){
          let page=getCurrentPages()
          if(page[page.length-1].route!='pages/myCenter/shopGoodsDetail'){
            return
          }
          that.judgeWriteOff(that)
        },6000)
      })
     }
    })
  },

   //判断是否被核销
   judgeWriteOff(that) {
    controller.judgeWriteOff({code:that.data.code}).then(res => {
      if(res.data==false){
        clearInterval(that.data.timer)
        this.setData({
          show:false
        })
        wx.showToast({
          icon:"none",
          title: '已核销',
        })
        setTimeout(function(){
          wx.navigateBack({
            delta: 0,
          })
        },1000)
      }
    })
  },

  cancle(){
    this.setData({
      show:false
    })
  },

  //获取店铺详情
  getShopDetail(){
    let value={}
    value.shopId=this.data.shopId
    controller.shopDetails(value).then(res=>{
      this.setData({
        shopDetail:res.data
      })
    })
  },
  //跳转店铺详情
  toShopDetail(e){
    let shopId=e.currentTarget.dataset.id
    wx.navigateTo({
      url: '/pages/Index/Index?shopId='+shopId,
    })
  },
  backBtn(){
    wx.navigateBack()
  },
  saveImg: function () {
    let that=this
    wx.getSetting({
      success(res) {
        //未授权 先授权 然后保存
        if (!res.authSetting['scope.writePhotosAlbum']) {
          wx.authorize({
            scope: 'scope.writePhotosAlbum',
            success(res) {
              that.saveToBlum();
            }
          })
        }else{
         //已授 直接调用保存到相册方法
          that.saveToBlum();
        }
      }
    })  
  },

  saveToBlum:function(){
    let that=this
    wx.downloadFile({
      url: that.data.codeUrl,
      success: function (res) {
        wx.saveImageToPhotosAlbum({
          filePath: res.tempFilePath,
          success(result) {
            wx.showToast({
              title: '保存成功',
              icon: 'success'
            })
          }
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      goodsId:options.id,
      shopId:options.shopId,
    })
    if(options.couponBagId){
      this.setData({
        couponBagId:options.couponBagId
      })
    }
    if(options.couponBagGoodsId){
      this.setData({
        couponBagGoodsId:options.couponBagGoodsId
      })
    }
    this.getGoodsDetail()
    this.getShopDetail()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let that=this
    this.setData({
      msgTimer:setInterval(function(){
        let page=getCurrentPages()
        if(page[page.length-1].route!='pages/myCenter/shopGoodsDetail'){
          return
        }
        console.log("1")
        that.getMsg()
      },10000)
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    let that=this
    clearInterval(that.data.msgTimer)
    clearInterval(that.data.timer)
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  // onShareAppMessage: function () {

  // }
})